home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 11,000 to 11,999 / 11000.zip / AOLDLs / Programmieren [Delphi - Units] / WAVEIN_ TWaveInOut / WAVEIN~1.pas < prev   
Pascal/Delphi Source File  |  2014-12-20  |  8KB  |  290 lines

  1. unit WaveInOut;
  2. { Copyright RL Software 1997. Homepage: http://members.aol.com/DReiche202/Welcome.htm/}
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   mmsystem;
  8.  
  9. const
  10.   WAVE_FORMAT_ADPCM  =  $0002;  //  Microsoft Corporation  */
  11.   WAVE_FORMAT_ALAW   =  $0006;  //  Microsoft Corporation  */
  12.  
  13. type TWaveInOutInputProc = procedure( buffer: pChar; size: Integer ) of object;
  14. type TWaveInOutPlayedProc = procedure of object;
  15.  
  16. type
  17.   TWaveInOut = class(TComponent)
  18.   private
  19.     { Private-Deklarationen }
  20.     WAVEIN:       HWAVEIN;
  21.     WAVEOUT:      HWAVEOUT;
  22.     WaveBuffer:   pChar;
  23.     WBuffer1:     pChar;
  24.     WBuffer2:     pChar;
  25.     AktBuffer:    Integer;
  26.     WAVEINHDR:    TWAVEHDR;
  27.     WAVEOUTHDR:   TWAVEHDR;
  28.     isInOpen:     Boolean;
  29.     isOutOpen:    Boolean;
  30.     FInputProc:   TWaveInOutInputProc;
  31.     FPlayedProc:  TWaveInOutPlayedProc;
  32.     FBufferSize:  Integer;
  33.     FUnlInput:    Boolean;
  34.     wincontrol:   TWinControl;
  35.     procedure     SetBuffer( size: Integer );
  36.   protected
  37.     { Protected-Deklarationen }
  38.     FABHandle:  THandle;
  39.     OldWndProc: TFarProc;
  40.     NewWndProc: Pointer;
  41.     procedure HookWin;
  42.     procedure UnHookWin;
  43.     procedure HookWndProc(var Message: TMessage);
  44.   public
  45.     { Public-Deklarationen }
  46.     constructor Create( AOwner: TComponent ); override;
  47.     destructor  Destroy; override;
  48.  
  49.     procedure   OpenInput;    // initialisiert und startet Aufzeichnung
  50.     procedure   CloseInput;   // beendet Aufzeichnung
  51.     procedure   RequestInput; // startet erneute Aufzeichnung
  52.  
  53.     procedure   OpenOutput;   // initialisiert Player
  54.     procedure   CloseOutput;  // beendet playing
  55.     procedure   PlaySound( buffer: pChar; len: Integer ); // gibt Speicherbereich akustisch aus
  56.   published
  57.     { Published-Deklarationen }
  58.     property OnInput:     TWaveInOutInputProc read FInputProc write FInputProc;
  59.     property OnPlayed:    TWaveInOutPlayedProc read FPlayedProc write FPlayedProc;
  60.     property BufferSize:  Integer read FBufferSize write SetBuffer default 4096;
  61.     property AutoRequest: Boolean read FUnlInput write FUnlInput default False;
  62.   end;
  63.  
  64. procedure Register;
  65.  
  66. implementation
  67.  
  68. procedure Register;
  69. begin
  70.   RegisterComponents('Extern', [TWaveInOut]);
  71. end;
  72.  
  73. constructor TWaveInOut.Create( AOwner: TComponent );
  74. begin
  75.   inherited Create( AOwner );
  76.   isInOpen    := False;
  77.   isOutOpen   := False;
  78.   wincontrol  := TWinControl.Create( Self );
  79.   wincontrol.Parent := Screen.Forms[0];
  80.   FABHandle := wincontrol.Handle;
  81.   HookWin();
  82. end;
  83.  
  84. destructor TWaveInOut.Destroy;
  85. begin
  86.   CloseInput();
  87.   CloseOutput();
  88.   UnHookWin();
  89.  
  90.   if ( WaveBuffer <> Nil ) and ( FBufferSize > 0 ) then
  91.     FreeMem( WaveBuffer );
  92.   inherited Destroy;
  93. end;
  94.  
  95. procedure TWaveInOut.SetBuffer( size: Integer );
  96. begin
  97.   if WBuffer1 <> Nil then
  98.   begin
  99.     FreeMem( WBuffer1 );
  100.     WBuffer1 := Nil;
  101.   end;
  102.  
  103.   if WBuffer2 <> Nil then
  104.   begin
  105.     FreeMem( WBuffer2 );
  106.     WBuffer2 := Nil;
  107.   end;
  108.  
  109.   FBufferSize := size;
  110.   GetMem( WBuffer1, FBufferSize );
  111.   GetMem( WBuffer2, FBufferSize );
  112.  
  113.   if AktBuffer = 0 then
  114.     WaveBuffer := WBuffer1
  115.   else
  116.     WaveBuffer := WBuffer2;
  117. end;
  118.  
  119. procedure TWaveInOut.OpenInput;
  120. var
  121.   wf: TWaveFormatEx;
  122.   res: MMRESULT;
  123. begin
  124.   with wf do
  125.   begin
  126.     wFormatTag          := WAVE_FORMAT_ALAW;
  127.     nChannels           := 1;
  128.     nSamplesPerSec      := 11025;
  129.     nAvgBytesPerSec     := 11025;
  130.     nBlockAlign         := 1;
  131.     wBitsPerSample      := 8;
  132.     cbSize              := SizeOf( TWaveFormatEx );
  133.   end;
  134.  
  135.   with WAVEINHDR do
  136.   begin
  137.     lpData          := WaveBuffer;              { pointer to locked data buffer }
  138.     dwBufferLength  := FBufferSize;             { length of data buffer }
  139.     dwBytesRecorded := 0;                       { used for input only }
  140.     dwFlags         := 0;                       { assorted flags (see defines) }
  141.     dwLoops         := 0;                       { loop control counter }
  142.   end;
  143.  
  144.   case waveInOpen( @WAVEIN, 0, @wf, FABHandle, 0, CALLBACK_WINDOW ) of
  145.     MMSYSERR_NOERROR:     isInOpen := True;
  146.   end;
  147.  
  148.   if isInOpen then
  149.   begin
  150.     waveInPrepareHeader( WAVEIN, @WAVEINHDR, sizeof( TWAVEHDR ) );
  151.     waveInAddBuffer( WAVEIN, @WAVEINHDR, sizeof( TWAVEHDR ) );
  152.     waveInStart( WAVEIN );
  153.   end;
  154. end;
  155.  
  156. procedure TWaveInOut.CloseInput;
  157. begin
  158.   if isInOpen then
  159.   begin
  160.     waveInStop( WAVEIN );
  161. (*
  162.     if assigned( FInputProc ) and ( WAVEINHDR.dwBytesRecorded > 0 ) then
  163.        FInputProc( WaveBuffer, WAVEINHDR.dwBytesRecorded );
  164. *)
  165.     waveInClose( WAVEIN );
  166.     isInOpen := False;
  167.   end;
  168. end;
  169.  
  170. procedure TWaveInOut.RequestInput;
  171. begin
  172.   if isInOpen then
  173.   begin
  174.     if AktBuffer = 0 then
  175.       WAVEINHDR.lpData := WBuffer2
  176.     else
  177.       WAVEINHDR.lpData := WBuffer1;
  178.  
  179.     waveInPrepareHeader( WAVEIN, @WAVEINHDR, sizeof( TWAVEHDR ) );
  180.     waveInAddBuffer( WAVEIN, @WAVEINHDR, sizeof( TWAVEHDR ) );
  181.     waveInStart( WAVEIN );
  182.   end;
  183. end;
  184.  
  185. procedure TWaveInOut.OpenOutput;
  186. var
  187.   wf: TWaveFormatEx;
  188.   res: MMRESULT;
  189. begin
  190.   with wf do
  191.   begin
  192.     wFormatTag          := WAVE_FORMAT_ALAW;
  193.     nChannels           := 1;
  194.     nSamplesPerSec      := 11025;
  195.     nAvgBytesPerSec     := 11025;
  196.     nBlockAlign         := 1;
  197.     wBitsPerSample      := 8;
  198.     cbSize              := SizeOf( TWaveFormatEx );
  199.   end;
  200.  
  201.   case waveOutOpen( @WAVEOUT, WAVE_MAPPER, @wf, FABHandle, 0, CALLBACK_WINDOW ) of
  202.     MMSYSERR_NOERROR:     isOutOpen := True;
  203.   end;
  204. end;
  205.  
  206. procedure TWaveInOut.CloseOutput;
  207. begin
  208.   if isOutOpen then
  209.   begin
  210.     waveOutClose( WAVEOUT );
  211.     isOutOpen := False;
  212.   end;
  213. end;
  214.  
  215. procedure TWaveInOut.PlaySound( buffer: pChar; len: Integer );
  216. begin
  217.   if isOutOpen then
  218.   begin
  219.     with WAVEOUTHDR do
  220.     begin
  221.       lpData          := buffer;             { pointer to locked data buffer }
  222.       dwBufferLength  := len;                { length of data buffer }
  223.       dwBytesRecorded := 0;                  { used for input only }
  224.       dwFlags         := 0;                  { assorted flags (see defines) }
  225.       dwLoops         := 0;                  { loop control counter }
  226.     end;
  227.  
  228.     waveOutPrepareHeader( WAVEOUT, @WAVEOUTHDR, sizeof( TWAVEHDR ) );
  229.     waveOutWrite( WAVEOUT, @WAVEOUTHDR, sizeof( TWAVEHDR ) );
  230.   end;
  231. end;
  232.  
  233. procedure TWaveInOut.HookWin;
  234. begin
  235.   OldWndProc := TFarProc(GetWindowLong(FABHandle, GWL_WNDPROC));
  236.   NewWndProc := MakeObjectInstance(HookWndProc);
  237.   SetWindowLong(FABHandle, GWL_WNDPROC, LongInt(NewWndProc));
  238. end;
  239.  
  240. procedure TWaveInOut.UnhookWin;
  241. begin
  242.   SetWindowLong( FABHandle, GWL_WNDPROC, LongInt(OldWndProc) );
  243.   if assigned( NewWndProc ) then
  244.     FreeObjectInstance( NewWndProc );
  245.   NewWndProc := nil;
  246. end;
  247.  
  248. procedure TWaveInOut.HookWndProc(var Message: TMessage);
  249. var
  250.   recorded: Integer;
  251. begin
  252.   with Message do
  253.   begin
  254.     case Msg of
  255.       MM_WIM_DATA:
  256.       begin
  257.         recorded := WAVEINHDR.dwBytesRecorded;
  258.  
  259.         if FUnlInput then
  260.           RequestInput();
  261.  
  262.         if assigned( FInputProc ) then
  263.           FInputProc( WaveBuffer, recorded );
  264.  
  265.         if FUnlInput then
  266.         begin
  267.           if AktBuffer = 0 then
  268.           begin
  269.             WaveBuffer  := WBuffer2;
  270.             AktBuffer   := 1;
  271.           end
  272.           else
  273.           begin
  274.             WaveBuffer  := WBuffer1;
  275.             AktBuffer   := 0;
  276.           end;
  277.         end;
  278.       end;
  279.       MM_WOM_DONE:
  280.       begin
  281.         if assigned( FPlayedProc ) then
  282.           FPlayedProc();
  283.       end;
  284.     end; // case .. of
  285.     result := CallWindowProc( OldWndProc, FABHandle, Msg, wParam, lParam );
  286.   end;
  287. end;
  288.  
  289. end.
  290.